<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册页面</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/register.css">

</head>
<body>
    <div class="nav">
        <img src="pic/blog-logo.png" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
    </div>

    <div class="container-register">
            <div class="register-dialog">
                    <form class="register-form" enctype="multipart/form-data">
                        <h2 class="form-title">用户注册</h2>

                        <!-- 用户名 -->
                        <div class="form-group">
                            <label for="username">用户名</label>
                            <input
                                    type="text"
                                    id="username"
                                    name="userName"
                                    required
                                    placeholder="请输入4-16位字母"
                            >
                        </div>

                        <!-- 密码 -->
                        <div class="form-group">
                            <label for="password">密码</label>
                            <input
                                    type="password"
                                    id="password"
                                    name="password"
                                    required
                                    placeholder="至少6位包含字母和数字"
                            >
                        </div>

                        <!-- 头像上传带预览 -->
                        <div class="form-group">
                            <label for="avatar">头像上传</label>
                            <div class="avatar-upload-container">
                                <!-- 预览区域 -->
                                <div class="avatar-preview">
                                    <img id="avatarPreview" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3C/svg%3E" alt="头像预览"/>
                                </div>

                                <!-- 上传控件 -->
                                <div class="upload-area">
                                    <input
                                            type="file"
                                            id="avatar"
                                            name="avatarPath"
                                            accept="image/png, image/jpeg"
                                            class="upload-input"
                                    />
                                    <div class="upload-ui">
                                        <svg class="upload-icon" viewBox="0 0 24 24">
                                            <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
                                        </svg>
                                        <div class="upload-text">
                                            <p class="upload-title">点击上传图片</p>
                                            <p class="upload-hint">支持PNG/JPG，最大2MB</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Gitee地址 -->
                        <div class="form-group">
                            <label for="gitee">Gitee地址</label>
                            <input
                                    type="url"
                                    id="gitee"
                                    name="githubUrl"
                                    placeholder="https://gitee.com/your-username"
                                    pattern="https://gitee.com/.*"
                            >
                        </div>

                        <button type="submit" class="submit-btn">立即注册</button>
                    </form>
            </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
    <script>
        // 仅保留一个submit事件绑定
        $('.register-form').on('submit', function(e) {
            e.preventDefault();

            // 统一验证（包含前端所有验证）
            if (!validateForm()) return;

            // 创建FormData
            const formData = new FormData(this); // 直接使用表单元素

            // 提交请求
            $.ajax({
                url: '/user/register',
                type: 'post',
                data: formData,
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $('.submit-btn').prop('disabled', true).text('注册中...');
                },
                complete: function() {
                    $('.submit-btn').prop('disabled', false).text('立即注册');
                },
                success: function(ret) {
                    if (ret.code === 'SUCCESS' && ret.data == true) {
                        alert('注册成功')
                        window.location.href = '/blog_login.html';
                    } else {
                        alert(ret.errMsg)
                    }
                },
                error: function() {
                    alert("注册失败，请检查网络")
                }
            });
        });

        // 完善的验证函数
        function validateForm() {
            // 用户名验证
            const username = $('#username').val();
            if (!/^[a-zA-Z]{4,16}$/.test(username)) {
                alert('用户名需4-16位字母');
                return false;
            }

            // 头像验证
            const file = $('#avatar')[0].files[0];
            if (!file) {
                alert('请上传头像');
                return false;
            }
            if (!file.type.match(/image\/(jpeg|png)/)) {
                alert('只支持JPG/PNG格式');
                return false;
            }
            if (file.size > 2 * 1024 * 1024) {
                alert('文件大小不能超过2MB');
                return false;
            }

            // Gitee地址验证
            const giteeUrl = $('#gitee').val();
            if (giteeUrl && !/^https:\/\/gitee.com\/.+/.test(giteeUrl)) {
                alert('请输入有效的Gitee地址');
                return false;
            }

            return true;
        }

        // 头像预览与即时验证
        $('#avatar').on('change', function(e) {
            const file = this.files[0];
            if (!file) return;

            // 即时类型验证
            if (!file.type.match(/image\/(jpeg|png)/)) {
                alert('只支持JPG/PNG格式');
                this.value = '';
                return;
            }

            // 即时大小验证
            if (file.size > 2 * 1024 * 1024) {
                alert('文件不能超过2MB');
                this.value = '';
                return;
            }

            // 更新预览
            const reader = new FileReader();
            reader.onload = function(e) {
                $('#avatarPreview').attr('src', e.target.result);
            };
            reader.readAsDataURL(file);
        });
    </script>
</body>
</html>